package edu.cs.networkmonitor.devices;

import java.net.Socket;
import java.util.Date;

public class PortSource extends BaseSource {

	public int _portNum;
	public String _hostName;

	public PortSource(String host, int port) {
		_hostName = host;
		_portNum = port;
		_sourceName = host + ":" + port;
	}
	
	@Override
	public boolean connect() {
		boolean status = false;
		System.out.println("Trying to connect to " + _sourceName + new Date().toString());
		try {
			Socket ServerSok = new Socket(_hostName, _portNum);
			System.out.println("Port in use: " + _hostName + _portNum);
			ServerSok.close();
			status = true;
		} catch (Exception e) {
			System.out.println("Port not in use: " + _hostName + _portNum);
			status = false;
		}
		_lastStatus = status;
		return status;
	}

	@Override
	public String getEmailBody() {
		return "Port connection lost: : \n\t\t" + _hostName + ":" + _portNum + " [" + _sourceDescription + " ]" + "\n Time: "+ new Date().toString();
	}

	@Override
	public String getEmailSubject() {
		return "Port connection lost:" + _portNum + " " + new Date().toString();
	}

}
